//
//  TCBlobDownloadManager.m
//
//  Created by Thibault Charbonnier on 16/04/13.
//  Copyright (c) 2013 Thibault Charbonnier. All rights reserved.
//

#import "TCBlobDownloadManager.h"
#import "NSString+hash.h"
#import "NSData+hash.h"

@interface TCBlobDownloadManager ()
{
    NSOperationQueue *_operationQueue;
    NSMutableDictionary *_set;
}

@end

@implementation TCBlobDownloadManager


#pragma mark - Init and utilities


- (id)init
{
    if (self = [super init]) {
        _operationQueue = [[NSOperationQueue alloc] init];
        //NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
        //_defaultDownloadPath = [paths objectAtIndex:0];
        _defaultDownloadPath = [NSString stringWithString:NSTemporaryDirectory()];
        _set=[[NSMutableDictionary alloc] init];
    }
    
    return self;
}

+ (id)sharedDownloadManager
{
    static dispatch_once_t onceToken;
    static id sharedManager = nil;
    
    dispatch_once(&onceToken, ^{
        sharedManager = [[[self class] alloc] init];
    });
    
    return sharedManager;
}

- (void)setDefaultDownloadPath:(NSString *)pathToDL
{
    if ([TCBlobDownload createPathFromPath:pathToDL])
        _defaultDownloadPath = pathToDL;
}

- (NSUInteger)downloadCount
{
    return [_operationQueue operationCount];
}

- (void)setMaxConcurrentDownloads:(NSInteger)maxConcurrent
{
    [_operationQueue setMaxConcurrentOperationCount:maxConcurrent];
}


#pragma mark - TCBlobDownloads Management


- (void)startDownloadWithURL:(NSURL *)url
                  customPath:(NSString *)customPathOrNil
                 andDelegate:(id<TCBlobDownloadDelegate>)delegateOrNil
{
    NSString *downloadPath = self.defaultDownloadPath;
    if (nil != customPathOrNil && [TCBlobDownload createPathFromPath:customPathOrNil])
        downloadPath = customPathOrNil;
    
    TCBlobDownload *downloader = [[TCBlobDownload alloc] initWithUrl:url
                                                        downloadPath:downloadPath
                                                         andDelegate:delegateOrNil];
    if (![self isInDownloadQueue:downloader]) {
        [_operationQueue addOperation:downloader];
    }
}

- (void)startDownloadWithURL:(NSURL *)url
                  customPath:(NSString *)customPathOrNil
          firstResponseBlock:(FirstResponseBlock)firstResponseBlock
               progressBlock:(ProgressBlock)progressBlock
                  errorBlock:(ErrorBlock)errorBlock
       downloadCanceledBlock:(DownloadCanceledBlock)downloadCanceledBlock
       downloadFinishedBlock:(DownloadFinishedBlock)downloadFinishedBlock
{
    NSString *downloadPath = self.defaultDownloadPath;
    if (nil != customPathOrNil && [TCBlobDownload createPathFromPath:customPathOrNil])
        downloadPath = customPathOrNil;
    
    TCBlobDownload *downloader = [[TCBlobDownload alloc] initWithUrl:url
                                                        downloadPath:downloadPath
                                                  firstResponseBlock:firstResponseBlock
                                                       progressBlock:progressBlock
                                                          errorBlock:errorBlock
                                               downloadCanceledBlock:downloadCanceledBlock
                                               downloadFinishedBlock:downloadFinishedBlock];
    if (![self isInDownloadQueue:downloader]) {
        [_operationQueue addOperation:downloader];
    }
}

- (void)startDownload:(TCBlobDownload *)blobDownload
{
    if (![self isInDownloadQueue:blobDownload]) {
        [_operationQueue addOperation:blobDownload];
    }
}

- (void)cancelAllDownloadsAndRemoveFiles:(BOOL)remove
{
    for (TCBlobDownload *blob in [_operationQueue operations])
        [blob cancelDownloadAndRemoveFile:remove];
#ifdef DEBUG
    NSLog(@"Cancelled all downloads.");
#endif
}

-(BOOL)isInDownloadQueue:(TCBlobDownload*)downloader{
    BOOL queued=NO;
    NSString *urlString=downloader.urlAdress.absoluteString;
    for (TCBlobDownload *blob in [_operationQueue operations]){
        NSString *queue=blob.urlAdress.absoluteString;
        if ([queue isEqualToString:urlString]) {
            queued=YES;
        }
    }
    return queued;
}

@end
